package com.suitsoft.ut.web.liti;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.entity.base.Teacher;
import com.suitsoft.ut.entity.base.Team;
import com.suitsoft.ut.entity.thesis.ExpertContent;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.thesis.ExpertContentManager;
import com.suitsoft.ut.service.thesis.ThesisFileManager;
import com.suitsoft.ut.util.StringUtils;
import com.suitsoft.ut.web.CrudActionSupport;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 * 专家审核立题
 *@author Suineg
 */
@Results(@Result(name=CrudActionSupport.RELOAD,location="expert-audit.action",type="redirect"))
public class ExpertAuditAction extends InitListActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4137423775864060081L;
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	
	/** 专家评语管理 服务层对象 */
	private ExpertContentManager expertContentManager;
	/** 流程管理 服务层对象 */
	private ThesisFileManager fileManager;
	
	
	/** 题的分页 */
	private Page<Map<String,Object>> page = new Page<Map<String,Object>>(Constants.PAGE_SIZE);
	/** 学院ID */
	private Long collegeId;
	/** 教师 */
	private String teacher;
	/** 团队 */
	private String group;
	/** 状态 */
	private Long stauts;
	/** 标识  1 - 评阅,2 - 团队,3 - 专家，4 - 基层 (专家没有评语。所以，专家审核时不会用到这个.)*/
	private Long  flag;
	/** 被选中ID */
	private List<Long> checkedIds;
	/** 专家评语 实体*/
	private ExpertContent expertContent;
	/** 批量审核时的审核信息 */
	private String multMsg;
	
	@Override
	public String execute() throws Exception {
		Teacher teacherObj = teacherManger.findTeacherByUser(UserHolder.getLoginUser().getId());
		if(teacherObj == null){
			addActionMessage("请使用教师账号登陆！");
			return SUCCESS;
		}
		boolean f = false;
		for(Team t : teacherObj.getTeamList()){
			if(Constants.TEAM_TYPE_LITI.equals(Integer.valueOf(t.getType()))){
				f = true;
			}
		}
		if(!f){
			addActionMessage("您未在立题团队中。不能进行此操作");
			return SUCCESS;
		}
		page = thesisManager.find(page, teacher, group, Constants.THSIS_STAUTS_LITI_SECOND_AUDIT,Constants.THESIS_FLAG_LITI);
		return SUCCESS;
	}
	/**
	 * 查看
	 * @return
	 */
	@Override
	public String input(){
		flag = 3L;
		return super.input();
	}
	@LogAnnotation(log = "立题 - 专家  - 保存评审")
	public String savePingshen(){
		if(thesisFile == null || thesisFile.getId() == 0l || flag == null || flag == 0l){
			addActionMessage("非法操作！");
			return RELOAD;
		}
		try {
			thesisManager.savePingShen(thesis, thesisFile, flag);
			addActionMessage("操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("保存失败！");
			return RELOAD;
		}
		return RELOAD;
	}
	@LogAnnotation(log = "立题 - 专家 - 批量审核")
	public String multSavePingshen(){
		flag = 3L;
		if(checkedIds == null || checkedIds.isEmpty() ||flag == null || flag == 0l){
			addActionMessage("非法操作！");
			return RELOAD;
		}
		try {
			List<Thesis> list = thesisManager.get(checkedIds);
			for(Thesis t : list){
				thesisFile = new ThesisFile();
				if(StringUtils.isEmpty(multMsg)){
					multMsg = "已审核";
				}
				thesisFile.setSetThePingyueContent(multMsg);
				thesisManager.savePingShen(t, thesisFile, flag);
			}
			addActionMessage("操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败！");
		}
		
		return RELOAD;
	}
	/**
	 * 更新标识 到退回
	 * @param thesis
	 * @throws Exception
	 */
	public void updateFileStatus(Long id) throws Exception{
		thesis = thesisManager.get(id);
		thesis.getThesisFile().setThesisStatus(Constants.THSIS_STAUTS_LITI_BACK_FIRST_AUDIT);
		thesisManager.save(thesis);
	}
	/**
	 * 添加专家评语
	 * @return
	 */
	@LogAnnotation(log = "立题 - 专家 - 添加评语")
	public String addExpertContent(){
		if(thesisFile == null || thesisFile.getId() == null){
			addActionMessage("非法操作！");
			return RELOAD;
		}
		thesisFile = fileManager.get(thesisFile.getId());
		expertContent.setAddDate(new Date());
		Teacher t = teacherManger.findTeacherByUser(UserHolder.getLoginUser().getId());
		String teacherName = null;
		if(t == null){
			teacherName = t.getTeacherName();
		}else{
			teacherName = UserHolder.getLoginUser().getName();
		}
		expertContent.setExpertName(teacherName);
		expertContent.setFile(thesisFile);
		
		expertContentManager.save(expertContent);
		
		id = thesisFile.getThesis().getId();
		return input();
	}
	public Page<Map<String, Object>> getPage() {
		return page;
	}
	public void setPage(Page<Map<String, Object>> page) {
		this.page = page;
	}
	public Long getCollegeId() {
		return collegeId;
	}
	public void setCollegeId(Long collegeId) {
		this.collegeId = collegeId;
	}
	public String getTeacher() {
		return teacher;
	}
	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	public Long getStauts() {
		return stauts;
	}
	public void setStauts(Long stauts) {
		this.stauts = stauts;
	}
	public Long getFlag() {
		return flag;
	}
	public void setFlag(Long flag) {
		this.flag = flag;
	}
	public List<Long> getCheckedIds() {
		return checkedIds;
	}
	public void setCheckedIds(List<Long> checkedIds) {
		this.checkedIds = checkedIds;
	}
	@Autowired
	public void setExpertContentManager(ExpertContentManager expertContentManager) {
		this.expertContentManager = expertContentManager;
	}
	public ExpertContent getExpertContent() {
		return expertContent;
	}
	public void setExpertContent(ExpertContent expertContent) {
		this.expertContent = expertContent;
	}
	@Autowired
	public void setFileManager(ThesisFileManager fileManager) {
		this.fileManager = fileManager;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public ThesisFile getThesisFile() {
		return thesisFile;
	}
	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}
	public String getMultMsg() {
		return multMsg;
	}
	public void setMultMsg(String multMsg) {
		this.multMsg = multMsg;
	}
}

